<?php

namespace App\Services\Statistics;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Fan;
use App\Models\GcUserImage;
use App\Models\Comment;
use App\Models\Product;
use App\Models\ProductSku;
use App\Models\VipPurchase;
use App\Models\VipCard;
use App\Models\GoldRecord;
use App\Models\Apple;
use App\Models\Balance;
use App\Models\Reward;
use App\Models\OrderRefund;
use App\Models\Withdraw;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;

class SearchCount
{


	public function searchUserCount($result)
	{

		if($result['gc_group_id']){
			return User::query()
			->where("gc_group_id","=",$result['gc_group_id'])
			->count();

		}else{

			return User::query()
			->count();
		}
		
	}


	public function searchFansCount($result)
	{

		if($result['gc_group_id']){
			return Fan::query()
					->join('users','users.id','=','fans.user_id')
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('users.gc_group_id',$result['gc_group_id'])
					->sum('money');

		}else{

			return Fan::query()
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->sum('money');
		}
		
	}

	public function searchXhsFansCount($result)
	{

		if($result['gc_group_id']){
			return Fan::query()
					->join('users','users.id','=','fans.user_id')
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'xhs')
					->where('users.gc_group_id',$result['gc_group_id'])
					->sum('money');

		}else{

			return Fan::query()
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'xhs')
					->sum('money');
		}
		
	}

	
	public function searchDyFansCount($result)
	{

		if($result['gc_group_id']){
			return Fan::query()
					->join('users','users.id','=','fans.user_id')
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'dy')
					->where('users.gc_group_id',$result['gc_group_id'])
					->sum('money');

		}else{

			return Fan::query()
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'dy')
					->sum('money');
		}
		
	}

public function searchSphFansCount($result)
	{

		if($result['gc_group_id']){
			return Fan::query()
					->join('users','users.id','=','fans.user_id')
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'sph')
					->where('users.gc_group_id',$result['gc_group_id'])
					->sum('money');

		}else{

			return Fan::query()
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'sph')
					->sum('money');
		}
		
	}

public function searchKsFansCount($result)
	{

		if($result['gc_group_id']){
			return Fan::query()
					->join('users','users.id','=','fans.user_id')
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'ks')
					->where('users.gc_group_id',$result['gc_group_id'])
					->sum('money');

		}else{

			return Fan::query()
					->where('fans.created_at', '>', $result['start'])
					->where('fans.created_at', '<', $result['end'])
					->where('fans.gc_type', 'ks')
					->sum('money');
		}
		
	}

	public function searchDividendImages($result)
	{

		if($result['gc_group_id']){
			return GcUserImage::query()
					->where('created_at', '>', $result['start'])
					->where('created_at', '<', $result['end'])
					->where('gc_group_id',$result['gc_group_id'])
					->where('user_id',">",0)
					->count();

		}else{

			return GcUserImage::query()
					->where('created_at', '>', $result['start'])
					->where('created_at', '<', $result['end'])
					->where('user_id',">",0)
					->count();
		}
		
	}

	public function searchXhsNum($result)
	{

		if($result['gc_group_id']){
			return User::query()
				->where("gc_group_id","=",$result['gc_group_id'])
				->sum('xhs_num');

		}else{

			return User::query()
				->sum('xhs_num');
		}
		
	}

	public function searchDyNum($result)
	{

		if($result['gc_group_id']){
			return User::query()
				->where("gc_group_id","=",$result['gc_group_id'])
				->sum('dy_num');

		}else{

			return User::query()
				->sum('dy_num');
		}
		
	}

	public function searchKsNum($result)
	{

		if($result['gc_group_id']){
			return User::query()
				->where("gc_group_id","=",$result['gc_group_id'])
				->sum('ks_num');

		}else{

			return User::query()
				->sum('ks_num');
		}
		
	}

	public function searchSphNum($result)
	{

		if($result['gc_group_id']){
			return User::query()
				->where("gc_group_id","=",$result['gc_group_id'])
				->sum('sph_num');

		}else{

			return User::query()
				->sum('sph_num');
		}
		
	}


	
}
